<?php
	
	include_once './config/settings.php';
	
	function DBConnect(){
	//TODO: Handle error cases during connect and execute query.
		if (@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) != true){
			print ("<H3> Failed to connect to database</H3>");
		}
	    
		mysql_select_db(DB_NAME);

	}
	
	
	function getSourceSupportedSites(){
		//TODO: Form JSON from data fetched from databases
		$supportedSites=executeQuery("select id,name,sourceURL,faviconlocation from SERVICE where sourcesupported=true;",'JSON');
		//$supportedSites=executeQuery("select id,name,sourceURL,faviconlocation from SERVICE where sourcesupported=true;");
		echo $supportedSites;
		// Enable below loop during degugging and Format not specified to execute query.
//		while ($sites=mysql_fetch_array($supportedSites)){
//			echo $sites[0]."===>".$sites[1]."<br>";	
//		}
	}
		
	function getTargetSupportedSites(){
		//TODO: Form JSON from data fetched from databases
		$supportedSites=executeQuery("select id,name,sourceURL,faviconlocation from SERVICE where targetsupported=true;",'JSON');
	}
	
	
	
	function getSourceItemAction($sourceid,$sourceName=NULL){
		//if sourcename is passed ignores source id.
		
		if ($sourceName != NULL)
		//Convert source name to upper before passing to DB(MySQL treats case insensitive need to take care for other)
			$supportedActions=executeQuery("select actiondisplayName,redirectURL from SERVICE_ACTIONS where issourcesupported=TRUE and serviceid=(select id from service where name='".$sourceName."');");
		else{
			$supportedActions=executeQuery("select actiondisplayName,redirectURL from SERVICE_ACTIONS where issourcesupported=TRUE and serviceid=$sourceid");
		}
// Enable below loop during degugging			
//		while ($actions=mysql_fetch_array($supportedActions)){
//			echo $actions[0]."===>".$actions[1]."<br>";	
//		}
	}
	
	

	function getTargetItemAction($targetid,$targetName=NULL){
		//if targetname is passed ignores target id.
		
		if ($targetName != NULL)
		//Convert source name to upper before passing to DB(MySQL treats case insensitive need to take care for other)
			$supportedActions=executeQuery("select actiondisplayName,redirectURL from SERVICE_ACTIONS where istargetsupported=TRUE and serviceid=(select id from service where name='".$targetName."');");
		else{
			$supportedActions=executeQuery("select actiondisplayName,redirectURL from SERVICE_ACTIONS where istargetsupported=TRUE and serviceid=$targetid");
		}
// Enable below loop during degugging
//		while ($actions=mysql_fetch_array($supportedActions)){
//			echo $actions[0]."===>".$actions[1]."<br>";	
//		}
	}
	
	function executeQuery($query,$format=null){
	  	DBConnect();
    	$result = mysql_query($query);
    	$JSONData=null;
    	if ($format == "JSON"){
    		//json_encode($result,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);
    		while ( $row=(mysql_fetch_array($result,1)) ) 		{
				//print_r($row);
    			$JSONData.=json_encode($row).",";
			}
			return $JSONData;
    	}
	    
    	return $result;
	}
	
	
	//DB interaction hook provided if third party plugin needs to perform DB interactions.
	function performDBAction($functionName,$query){
		//TODO: Call custom function registered by user.
		
		executeQuery($query);
		
	}
	
	
	
	
	
?>
